#include "list.h"
#include <stdio.h>

struct class {
    list_node students;
};

struct student {
    char * name;
    int age;
    list_node node;
};


int main(int argc, char * argv[])
{
    struct class class;
    struct student student1 = {"zhangsan", 18};
    struct student student2 = {"lisi", 23};
    struct student student3 = {"wangmazi", 34};

    list_init(&class.students);
    printf("---------------------------\n");
    printf("add student 1, 2, 3\n");
    list_add(&class.students, &student1.node);
    list_add(&class.students, &student2.node);
    list_add(&class.students, &student3.node);
    for (list_node * node = (&class.students)->next; node != &class.students; node = node->next) {
        // size_t offset = sizeof(struct student) - sizeof(list_node);
        // struct student * student = (struct student *)((uint8_t *)node - offset);
        struct student * student = LIST_ENTRY(node, struct student);
        printf("name: %s, age: %d\n", student->name, student->age);
    }

    printf("---------------------------\n");
    printf("delete student 1, 2, 3\n");
    list_del(&student1.node);
    list_del(&student2.node);
    list_del(&student3.node);
    for (list_node * node = (&class.students)->next; node != &class.students; node = node->next) {
        struct student * student = LIST_ENTRY(node, struct student);
        printf("name: %s, age: %d\n", student->name, student->age);
    }

    printf("---------------------------\n");
    printf("tail add student 1, 2, 3\n");
    list_tailadd(&class.students, &student1.node);
    list_tailadd(&class.students, &student2.node);
    list_tailadd(&class.students, &student3.node);
    for (list_node * node = (&class.students)->next; node != &class.students; node = node->next) {
        struct student * student = LIST_ENTRY(node, struct student);
        printf("name: %s, age: %d\n", student->name, student->age);
    }
    return 0;
}
